package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.util.ApiUtils;
import com.atguigu.yygh.model.hosp.Hospital;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(value = "对外调用医院接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {

    @Autowired
    private HospitalService hospitalService;


    @ApiOperation(value = "查询医院")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        //转换map并检查sign
        Map<String, Object> paramMap = ApiUtils.getMapAndCheckSign(request);
        //转换以及判断签名完成 进行查询
        Hospital hospital = hospitalService.getByHoscode(((String) paramMap.get("hoscode")));
        return Result.ok(hospital);
    }

    @ApiOperation(value = "保存医院")
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //转换map并检查sign
        Map<String, Object> paramMap = ApiUtils.getMapAndCheckSign(request);
        //传输过程中 "+" 转化为了空格 所以我们要转回来
        changeLogoData(paramMap);
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //转换
    private void changeLogoData(Map<String, Object> paramMap) {
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logData", logoData);
    }
}
